﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using System.Web;
using System.Web.Mvc;
using WebApplication1.Filters;
using WebApplication1.Models;
using WebApplication1.ViewModels;

namespace WebApplication1.Controllers
{



    /// <summary>
    /// 文件上传控制
    /// </summary>
    public class BulkUploadController : AsyncController
    {
        // GET: BulkUpload
        [HeaderFooterFilter]
        [AdminFilter]
        public ActionResult Index()
        {
            return View(new FileUploadViewModel());
        }

        //public ActionResult Upload(int id)
        //{
        //    //value of id will be 5 -> int 5
        //    return View();
        //}
        //public ActionResult Upload(string id)
        //{
        //    //value of id will be 5 -> int 5
        //    return View();
        //}
        //public ActionResult Upload(string MyID)
        //{
        //    //value of id will be 5 -> int 5
        //    return View();
        //}

        [AdminFilter]
        [HandleError]
        public async Task<ActionResult> Upload(FileUploadViewModel model)
        {
            int t1 = Thread.CurrentThread.ManagedThreadId;
            List<Employee> employees = await Task.Factory.StartNew<List<Employee>>
           (() => GetEmployees(model));//开启新线程
            int t2 = Thread.CurrentThread.ManagedThreadId;
            EmployeeBusinessLayer bal = new EmployeeBusinessLayer();
            bal.UploadEmployees(employees);
            return RedirectToAction("Index", "Employee");
        }


        [HandleError(View = "DivideError", ExceptionType = typeof(DivideByZeroException))]
        [HandleError(View = "NotFiniteError", ExceptionType = typeof(NotFiniteNumberException))]
        [HandleError(View = "MyError")]
        private List<Employee> GetEmployees(FileUploadViewModel model)

        {
            List<Employee> employees = new List<Employee>();
            StreamReader csvreader = new StreamReader(model.fileUpload.InputStream);
            csvreader.ReadLine(); // Assuming first line is header
            while (!csvreader.EndOfStream)
            {
                var line = csvreader.ReadLine();
                var values = line.Split(',');//Values are comma separated
                Employee e = new Employee();
                e.FirstName = values[0];
                e.LastName = values[1];
                e.Salary = int.Parse(values[2]);
                employees.Add(e);
            }
            return employees;
        }

       
    }
}